package com.example.demo.service.impl;

import com.example.demo.domain.BudgetActual;
import com.example.demo.domain.BudgetType;
import com.example.demo.mapper.BudgetActualMapper;
import com.example.demo.service.BudgetActualService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class BudgetActualServiceImpl implements BudgetActualService {

    @Resource
    private BudgetActualMapper budgetActualMapper;

    @Override
    public Map<String, Object> getAllCost(String type) {
        Calendar cal = Calendar.getInstance();
        int month = cal.get(Calendar.MONTH) + 1;
        Map<String, Object> map = new HashMap<>();
        List<BudgetActual> list = budgetActualMapper.getAllCost(type, month);
        map.put("list", list);
        map.put("targetAmount", list.stream().mapToDouble(BudgetActual::getTargetAmount).sum());
        map.put("nowMonthAmount", list.stream().mapToDouble(BudgetActual::getNowMonthAmount).sum());
        map.put("sumAmount", list.stream().mapToDouble(BudgetActual::getSumAmount).sum());
        map.put("lastMonthAmount", list.stream().mapToDouble(BudgetActual::getLastMonthAmount).sum());
        return map;
    }

    @Override
    public Map<String, Object> getCostSum(String type) {
        Calendar cal = Calendar.getInstance();
        int month = cal.get(Calendar.MONTH) + 1;
        List<BudgetActual> list = budgetActualMapper.getCostSum(type, month);
        Map<String, Object> map = new HashMap<>();
        map.put("list", list);
        map.put("targetAmount", list.stream().mapToDouble(BudgetActual::getTargetAmount).sum());
        map.put("nowMonthAmount", list.stream().mapToDouble(BudgetActual::getNowMonthAmount).sum());
        map.put("sumAmount", list.stream().mapToDouble(BudgetActual::getSumAmount).sum());
        map.put("lastMonthAmount", list.stream().mapToDouble(BudgetActual::getLastMonthAmount).sum());
        return map;
    }

    @Override
    public BudgetActual getActualMonth(int id) {
        return budgetActualMapper.getActualMonth(id);
    }

    @Override
    public int save(List<BudgetActual> budgetActual) {
        int num = 0;
        for (BudgetActual item : budgetActual){
            if (item.getId() != 0) {
                num += budgetActualMapper.updateTarget(item);
            } else {
                BudgetType type = new BudgetType();
                type.setProject(item.getProject());
                type.setType(item.getTypeId());
                int n = budgetActualMapper.insertType(type);
                System.out.println(n);
                item.setTypeId(type.getId());
                num += budgetActualMapper.saveTarget(item);
                BudgetActual actual = new BudgetActual();
                actual.setTypeId(type.getId());
                num += budgetActualMapper.saveActual(actual);
            }
        }
        return num;
    }

    @Override
    public int saveActual(List<BudgetActual> budgetActual) {
        int num = 0;
        for (BudgetActual item : budgetActual){
            if (item.getId() != 0) {
                num += budgetActualMapper.updateActual(item);
            } else {
                BudgetType type = new BudgetType();
                type.setProject(item.getProject());
                type.setType(item.getTypeId());
                int n = budgetActualMapper.insertType(type);
                System.out.println(n);
                item.setTypeId(type.getId());
                num += budgetActualMapper.saveActual(item);
                BudgetActual actual = new BudgetActual();
                actual.setTypeId(type.getId());
                num += budgetActualMapper.saveTarget(actual);
            }
        }
        return num;
    }

    @Override
    public Map<String, Object> getTarget(String type) {
        Calendar cal = Calendar.getInstance();
        int month = cal.get(Calendar.MONTH) + 1;
        Map<String, Object> map = new HashMap<>();
        List<BudgetActual> list = budgetActualMapper.getTarget(type, month);
        map.put("list", list);
        map.put("targetAmount", list.stream().mapToDouble(BudgetActual::getTargetAmount).sum());
        map.put("nowMonthAmount", list.stream().mapToDouble(BudgetActual::getNowMonthAmount).sum());
        map.put("sumAmount", list.stream().mapToDouble(BudgetActual::getSumAmount).sum());
        return map;
    }

    @Override
    public int delTarget(int id) {
        return budgetActualMapper.delTarget(id);
    }

    @Override
    public List<BudgetActual> getResult(String type) {
        return budgetActualMapper.getResult(type);
    }

    @Override
    public List<BudgetActual> getDataList(String type) {
        return budgetActualMapper.getDataList(type);
    }
}




